/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.gantt.items;

import org.tbjd.client.beans.Task;
import org.tbjd.client.interfaces.Builder;

import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class GanttPhaseItem extends AbstractGanttItem implements Builder {

	/**
	 * The container representing a phase
	 */
	private FocusPanel container = new FocusPanel();

	public GanttPhaseItem() {
	}

	public GanttPhaseItem(Task task) {
		setBean(task);
		build();
		initWidget(this.container);
	}

	public void build() {
		this.container.add(new Label(((Task) getBean()).getName()));
		this.container.setStyleName("ganttPhaseItem");
		setHandle(this.container);
		setYCoordinate(((Task) getBean()).getLevel());
	}

	/**
	 * <p>
	 * Computes the length in pixels for the GanttPhaseItem
	 * </p>
	 * <p>
	 * At instantiation, this method is useless because we don't have the xCoordinates of children (GanttTaskItems) yet
	 * </p>
	 */
	private void computeWidth() {
		this.container.setWidth(((Task) this.getBean()).getDuration() * 60 / 24 + "px");
	}

	public void addChildItem(AbstractGanttItem child) {
		super.addChildItem(child);
		computeWidth();
	}

	public long getId() {
		return ((Task) getBean()).getId();
	}

	public Widget getBuildedWidget() {
		return this.container;
	}

}
